﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Boxi.Model;

namespace Boxi.Extensions
{
    public static partial class MoveModelExtensions
    {
        public static Byte[] GetPointsThatWillScore(this MoveModel[] moveOptions, Byte moveID, GameModel game)
        {
            var points = new List<Byte>(2);
            Byte firstPoint = moveOptions.GetByID(moveID).FirstPoint;
            if (firstPoint > 0)
            {
                var first = new PointModel(firstPoint, game.Columns);
                if (WillMoveScore(moveOptions, first, moveID))
                    points.Add(first.PointID);
            }
            var secondPoint = moveOptions.GetByID(moveID).SecondPoint;
            if (secondPoint > 0)
            {
                var second = new PointModel(secondPoint, game.Columns);
                if (WillMoveScore(moveOptions, second, moveID))
                    points.Add(second.PointID);
            }
            return points.ToArray();
        }
        private static Boolean WillMoveScore(this MoveModel[] moveOptions, PointModel second, Byte moveID)
        {
            Byte result = 0;
            for (Byte i = 0; i < 4; i++)
            {
                var currentSide = moveOptions.GetByID(second[i]);
                if ((currentSide.MoveID == moveID && currentSide.MoveMadeOrder == 0) || (currentSide.MoveID != moveID && currentSide.MoveMadeOrder != 0))
                    result += 1;
            }
            return (result == 4);
        }
        public static MoveModel GetByID(this MoveModel[] movesMade, Byte moveID)
        {
            for (Byte i = 0; i < movesMade.Length; i++)
            {
                if (movesMade[i].MoveID == moveID)
                    return movesMade[i];
            }
            return default(MoveModel);
        }
        public static Byte[] GetOpenMoveIDs(this MoveModel[] moves)
        {
            var openMoveIDs = new List<Byte>();
            for (Byte i = 0; i < moves.Length; i++)
            {
                if (moves[i].MoveMadeOrder != 0)
                    openMoveIDs.Add(moves[i].MoveID);
            }
            return openMoveIDs.ToArray();
        } 
    }
}
